package selenium;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;

import org.apache.commons.lang3.StringUtils;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;

public class daguan2 {
	// 去除下载重复的视频       
	//private static final String baidu = "115.239.211.112";

	public static void main(String[] args) {
		
		
		//getNetworkState(baidu);
		// *********************唯一需要更改的地方********************
		String filepath = "C:\\新建文件夹";// E:\分类视频采集王\好看视频\搞笑
		// *********************唯一需要更改的地方********************

		
			String filepath1 = filepath;
			File file = new File(filepath);
			if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					System.gc();
					System.out.println("循环"+i+"/"+filelist.length);
					String fileName = filelist[i];
					File fileDel = new File(filepath1+"/"+fileName);
					if (fileName.endsWith(".txt")) {
						continue;
					}
					
					if (fileName.length()<5) {
						fileDel.delete();
						System.out.println("视频为空标题的直接删除");
						continue;
					}
					
					String endWith123 = fileName.split(".mp4")[0];
					System.out.println(fileName);
					
					String str123 = new String(endWith123);
					str123=endWith123.substring(endWith123.length()-1,endWith123.length());
					if (isNum(str123)) {
						fileDel.delete();
						System.out.println("末位为数字的视频删除成功");
						continue;
					}
					
					if (endWith123.endsWith(")")) {
						fileDel.delete();
						System.out.println("重复下载的视频删除成功");
						continue;
					}
					if (endWith123.length()<15||endWith123.length()>50) {
						fileDel.delete();
						System.out.println("文件名小于15大于50删除成功");
						continue;
					}
					if (fileName.endsWith(".vd!")) {
						fileDel.delete();
						System.out.println("维棠未下载完全的删除成功");
						continue;
					}
					if (fileDel.length()>99999999||fileDel.length()<300000) {
						fileDel.delete();
						System.out.println("文件过大或者过小，删除成功");
						continue;
					}
					if (fileName.endsWith(".ts")||fileName.endsWith(".f4v")) {
						File fileReName = new File(filepath1+"/"+fileName);
						File mm=new File(filepath1+"/"+endWith123.split(".f4v")[0].split(".ts")[0]+".mp4");
						fileReName.renameTo(mm);
						System.out.println("文件改名成功");
						filelist[i] = endWith123.split(".f4v")[0].split(".ts")[0] + ".mp4";
						i--;
						continue;
					}
					
					try {
						File source = new File(filepath1+"/"+fileName);
						if(source.exists()){
						Encoder encoder = new Encoder();
						FileChannel fc= null;
						String size = "";
						
						it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);
						long ls = m.getDuration();
						System.out.println("视频名称为:"+fileName);
						System.out.println("此视频时长为:"+ls/60000+"分"+(ls)%60000/1000+"秒！");
						//视频帧宽高
						if (m.getVideo()!=null) {
							System.out.println("此视频高度为:"+m.getVideo().getSize().getHeight());
							System.out.println("此视频宽度为:"+m.getVideo().getSize().getWidth());
							if (m.getVideo().getSize().getHeight()<320||m.getVideo().getSize().getWidth()<320) {
								fileDel.delete();
								System.out.println("视频帧高度或者宽度低于320，删除成功");
								continue;
							}
						}else{
							fileDel.delete();
							System.out.println("视频帧高度和宽度读不出来，删除成功");
							continue;
						}
						String videoFormat = m.getFormat();
						System.out.println("此视频格式为:"+videoFormat);
						if(!"mov".equals(videoFormat)){
							fileDel.delete();
							System.out.println("非视频格式的要删除，删除成功");
							continue;
						}
						@SuppressWarnings("resource")
						FileInputStream fis = new FileInputStream(source);
						fc= fis.getChannel();
						BigDecimal fileSize = new BigDecimal(fc.size());
						size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
						System.out.println("此视频大小为"+size);
						}
					} catch (InputFormatException e) {
						fileDel.delete();
						System.out.println("视频播放不出来，文件已经损坏，直接删除");
						e.printStackTrace();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (EncoderException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println("所有视频处理完毕");
		}	
	}
	public static boolean isNum(String str){ 
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$"); 
	} 
	
	private static boolean getNetworkState(String ip) {
		Runtime runtime = Runtime.getRuntime();
		try {
			Process process = runtime.exec("ping " + ip);
			InputStream iStream = process.getInputStream();
			InputStreamReader iSReader = new InputStreamReader(iStream, "UTF-8");
			BufferedReader bReader = new BufferedReader(iSReader);
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = bReader.readLine()) != null) {
				sb.append(line);
			}
			iStream.close();
			iSReader.close();
			bReader.close();
			String result = new String(sb.toString().getBytes("UTF-8"));
			if (!StringUtils.isBlank(result)) {
				if (result.indexOf("TTL") > 0 || result.indexOf("ttl") > 0) {
					return true;//有网
				} else {
					return false;//没有网
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;//没有网
	
	}
}
